package com.freemodbus4j.core.frame;

import lombok.Builder;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;

@Data
@Builder
public class TcpFrame extends ModbusFrame {


    @Setter
    @Getter
    @Builder
    public static class Request extends ModbusFrame {
        /**
         * MBAP header 报文头
         */
        private Header header;

        private RequestBody body;

        private Except except;

    }

    @Setter
    @Getter
    @Builder
    public static class Response extends ModbusFrame {
        /**
         * MBAP header 报文头
         */
        private Header header;

        private ResponseBody body;

        private Except except;

    }

    @Setter
    @Getter
    @Builder
    public static class TcpHeader extends Header {

        /**
         * 传输标识 每次自动+1
         */
        private short transactionId;

        /**
         * 协议标识
         */
        private short protocolId;

        /**
         * 字节长度 Hi高位 设置为X00  Lo低位记录后续的资金个数
         */
        private short length;

        /**
         * 单元标识
         */
        protected byte unitId;


        /**
         * 获取头字节数组
         * @return
         */
        @Override
        public byte[] getHeaderBytes() {
            return new byte[]{
                    (byte)(transactionId >> 8 & 0xFF), (byte)(transactionId & 0xFF),
                    (byte)(protocolId >> 8 & 0xFF), (byte)(protocolId & 0xFF),
                    (byte)(length >> 8 & 0xFF), (byte)(length & 0xFF),
                    unitId
            };
        }
    }



}
